<template>
  <van-popup v-model:show="show" position="bottom" round>
    <div class="pop">
      <div class="header van-hairline--bottom">
        请选择类型
        <van-icon name="cross" class="cross" @click="toggle" />
      </div>
      <div class="content">
        <div
          @click="choseType({ id: 'all' })"
          :class="{ all: true, active: active == '全部类型' }"
        >
          全部类型
        </div>
        <div class="title">支出</div>
        <div class="expense-wrap">
          <p
            @click="choseType(item)"
            :class="{ active: active == item.id }"
            v-for="item in expense"
            :key="item"
          >
            {{ item.name }}
          </p>
        </div>
        <div class="title">收入</div>
        <div class="income-wrap">
          <p
            @click="choseType(item)"
            :class="{ active: active == item.id }"
            v-for="item in income"
            :key="item"
          >
            {{ item.name }}
          </p>
        </div>
      </div>
    </div>
  </van-popup>
</template>

<script>
import { reactive, toRefs, onMounted } from 'vue'
import { get } from '@/utils/request'
export default {
  name: 'PopType',
  props: {
    selectType: {
      selectType: Function
    }
  },
  setup(props, { emit }) {
    const state = reactive({
      active: '全部类型',
      show: false,
      expense: [],
      income: []
    })
    const toggle = () => {
      state.show = !state.show
    }
    const choseType = (item) => {
      state.active = item.id
      state.show = false
      emit('selectType', item)
    }
    onMounted(async () => {
      const { data } = await get('type/list')
      state.expense = data.list.filter((v) => v.type == 1)
      state.income = data.list.filter((v) => v.type == 2)
    })
    return {
      ...toRefs(state),
      toggle,
      choseType
    }
  }
}
</script>

<style lang="less" scoped>
.pop {
  position: relative;
  background-color: var(--bg-color1);
  .header {
    position: sticky;
    top: 0;
    left: 0;
    z-index: 1000;
    width: 100%;
    height: 56px;
    text-align: center;
    font-size: 14px;
    line-height: 56px;
    color: var(--text-color1);
    background-color: var(--bg-color4);
    .cross {
      position: absolute;
      left: 10px;
      top: 50%;
      font-size: 20px;
      transform: translateY(-50%);
      color: var(--text-color1);
    }
  }
  .content {
    padding: 20px;
    .all {
      display: inline-block;
      padding: 12px 20px;
      font-size: 16px;
      color: var(--text-color1);
      background-color: var(--bg-color4);
    }
    .title {
      color: var(--text-color1);
      margin: 10px 0;
      font-size: 14px;
    }
    .expense-wrap,
    .income-wrap {
      display: flex;
      justify-content: space-between;
      flex-wrap: wrap;
      p {
        width: calc(~'(100% - 20px) / 3');
        text-align: center;
        padding: 12px 0;
        margin-bottom: 10px;
        background-color: var(--bg-color4);
        font-size: 16px;
      }
    }
    .active {
      background-color: var(--bg-color3) !important;
      color: var(--text-color2);
    }
  }
}
</style>
